package provider

import (
	"context"
	"errors"
)

// Provider types.
const (
	TypeOpenAI = "openai"
	TypeOllama = "ollama"
)

// Request captures provider specific inputs.
type Request struct {
	Prompt   string
	Metadata map[string]string
}

// Response encapsulates provider output.
type Response struct {
	Content string
	LatencyMs float64
	Cost float64
	Metadata map[string]string
}

// Provider defines the behaviour each AI backend must implement.
type Provider interface {
	Analyze(ctx context.Context, req Request) (Response, error)
}

// Registry maps provider identifiers to implementations.
type Registry map[string]Provider

// Resolve picks a provider or returns error if not found.
func (r Registry) Resolve(name string) (Provider, error) {
	if provider, ok := r[name]; ok {
		return provider, nil
	}
	return nil, errors.New("provider not registered")
}

// NewOpenAIProvider constructs an OpenAI provider placeholder.
func NewOpenAIProvider(apiKey string) Provider {
	return &noopProvider{identifier: TypeOpenAI}
}

// NewOllamaProvider constructs an Ollama provider placeholder.
func NewOllamaProvider(host string) Provider {
	return &noopProvider{identifier: TypeOllama}
}

// noopProvider provides a simple placeholder implementation.
type noopProvider struct {
	identifier string
}

func (p *noopProvider) Analyze(ctx context.Context, req Request) (Response, error) {
	return Response{
		Content:   "AI provider not yet implemented",
		LatencyMs: 0,
		Cost:      0,
		Metadata: map[string]string{
			"provider": p.identifier,
		},
	}, nil
}
